package com.lz.ionicbase.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TimePicker;

import com.lz.ionicbase.R;
import com.lz.ionicbase.utils.DpUtil;

import java.util.Calendar;

/**
 * Created by wyf on 2016/8/11.
 */
public class WheelDatePickerDialog {


    private Context context;
    private Dialog pickDialog;

    private OnWheelDateDismiss onWheelDateDismiss;
//    private TextView xingzuo_tv;
//    private TextView age_tv;
    private DatePicker datePicker1;
    private TimePicker timePicker1;

    public void showDatePickerDialog(Activity activity, OnWheelDateDismiss onWheelDateDismiss) {

        this.context = activity;
        this.onWheelDateDismiss = onWheelDateDismiss;

        pickDialog = new Dialog(activity, R.style.dialog_float);
        pickDialog.setContentView(R.layout.dia_timepicker);

        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        Window dlgwin = pickDialog.getWindow();
        WindowManager.LayoutParams lp = dlgwin.getAttributes();
        dlgwin.setGravity(Gravity.BOTTOM);
        lp.width = (int)(display.getWidth()); //设置宽度

        pickDialog.getWindow().setAttributes(lp);

        datePicker1 = (DatePicker) pickDialog.findViewById(R.id.datePicker1);
        timePicker1 = (TimePicker) pickDialog.findViewById(R.id.timePicker1);
        timePicker1.setIs24HourView(true);
        resizeDatePicker(datePicker1);
        resizeTimePicker(timePicker1);

        // 获取日历对象
        Calendar calendar = Calendar.getInstance();
        // 获取当前对应的年、月、日的信息
        int year = calendar.get(Calendar.YEAR);
        //int month = calendar.get(Calendar.MONTH) + 1;
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        datePicker1.init(year, month, day,null);


        pickDialog.setOnDismissListener(onDismissListener);
        pickDialog.show();

    }

    private void resizeTimePicker(TimePicker timePicker){
        resizeNumberPicker(timePicker, "hour");
        resizeNumberPicker(timePicker, "minute");
    }

    private void resizeDatePicker(DatePicker datePicker){
        resizeNumberPicker(datePicker, "day");
        resizeNumberPicker(datePicker, "month");
        resizeNumberPicker(datePicker, "year");
    }

    private void resizeNumberPicker(ViewGroup pickerContainer , String idName){

        int id = getInnerId(idName, "id");//com.android.internal.R.id.day
        Log.w("WheelDatePickerDialog", "id = "+id);
        if(id==0){
            return;
        }

        NumberPicker numberPickerSpinner = (NumberPicker) pickerContainer.findViewById(id);
        int numberpicker_inputID = getInnerId("numberpicker_input", "id");
        EditText mDaySpinnerInput = (EditText) numberPickerSpinner.findViewById(numberpicker_inputID);
        mDaySpinnerInput.setTextSize(17);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DpUtil.dip2px(context,50),
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);
        numberPickerSpinner.setLayoutParams(params);
        numberPickerSpinner.setPadding(0,0,0,0);
    }

    private int getInnerId(String idName, String defType) {
        Resources mResources = Resources.getSystem();
        int id = mResources.getIdentifier(idName, defType, "android");
        return id;
    }

    private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            onWheelDateDismiss.onWheelDateDismiss("", "", datePicker1.getYear()+"-"+(datePicker1.getMonth()+1)
                    +"-"+datePicker1.getDayOfMonth()+" "
            +fTime(timePicker1.getCurrentHour())+":"+fTime(timePicker1.getCurrentMinute())+":00");
            onWheelDateDismiss = null;
        }
    };

    private String fTime(int t){
        if(t<10){
            return "0"+t;
        }
        return ""+t;
    }

    public interface OnWheelDateDismiss {
        public void onWheelDateDismiss(String age, String xingZuo, String date);
    }

    public static int getAge(int year, int month, int day){
        Calendar calendar = Calendar.getInstance();
        int age = calendar.get(Calendar.YEAR)- year;
        ;
        if(calendar.getInstance().get(Calendar.MONTH) + 1 - month < 0){
            age--;
        }else if(calendar.getInstance().get(Calendar.MONTH) + 1 - month == 0){
            if(calendar.get(Calendar.DAY_OF_MONTH) - day < 0){
                age--;
            }
        }
        return age;
    }
    public static String getXingZuo(int month, int day) {

        String[] xingZuos = new String[]{"魔羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座"};

        int index = month-1;
        switch (month) {
            case 2:
                if (day > 19) index++;
                break;

            case 1:
            case 3:
            case 4:
                if (day > 20) index++;
                break;

            case 5:
            case 6:
            case 12:
                if (day > 21) index++;
                break;

            case 7:
            case 8:
            case 9:
            case 11:
                if (day > 22) index++;
                break;

            case 10:
                if (day > 23) index++;
                break;
            default: throw  new RuntimeException("请输入正确的时间!");
        }

        return xingZuos[index];
    }
}
